/*
 * MapParamSetDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package org.keyroy.map_editor.ui.dialog;

import javax.swing.JOptionPane;

import org.keyroy.map_editor.map.Map;

/**
 * 
 * @author __USER__
 */
public class MapParamSetDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;

	private OnMapParamSetListener listener;

	/** Creates new form MapParamSetDialog */
	public MapParamSetDialog(java.awt.Frame parent, String name, OnMapParamSetListener listener) {
		super(parent, true);
		this.listener = listener;
		initComponents();
		jTextField1.setText(name);
	}

	public MapParamSetDialog(java.awt.Frame parent, Map map, OnMapParamSetListener listener) {
		this(parent, map.getName(), listener);
		jTextField1.setEditable(false);
		colNum.setText(String.valueOf(map.getColumns()));
		rowNum.setText(String.valueOf(map.getRows()));
		tWidth.setText(String.valueOf(map.getGridWidth()));
		tHeight.setText(String.valueOf(map.getGridHeight()));

		jCheckBox1.setSelected(map.used45);
	}

	private MapParamSetDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		colNum = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		rowNum = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		tHeight = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		tWidth = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		jCheckBox1 = new javax.swing.JCheckBox();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("\u8f93\u5165\u5730\u56fe\u53c2\u6570");

		jLabel1.setText("\u540d\u79f0 : ");

		jTextField1.setText("m1");

		jLabel2.setText("\u884c\u5217 : ");

		colNum.setText("50");

		jLabel3.setText(" X ");

		rowNum.setText("50");

		jButton1.setText("\u521b\u5efa\u5730\u56fe");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		tHeight.setText("32");

		jLabel4.setText(" X ");

		tWidth.setText("32");

		jLabel5.setText("\u5927\u5c0f : ");

		jCheckBox1.setText("\u4f7f\u752845\u5ea6\u89d2\u6a21\u5f0f");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								layout.createSequentialGroup().addComponent(jLabel1).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTextField1,
										javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)).addGroup(
								layout.createSequentialGroup().addComponent(jLabel2).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(colNum,
										javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
												jLabel3).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(rowNum, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
								.addGroup(
										layout.createSequentialGroup().addComponent(jLabel5).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
												layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
														.addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(tWidth, javax.swing.GroupLayout.DEFAULT_SIZE, 136,
																Short.MAX_VALUE)).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel4)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
														layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE,
																		158, Short.MAX_VALUE).addComponent(tHeight,
																		javax.swing.GroupLayout.DEFAULT_SIZE, 158,
																		Short.MAX_VALUE)))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
								.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2)
										.addComponent(jLabel3).addComponent(colNum, javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(rowNum, javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel5)
										.addComponent(jLabel4).addComponent(tWidth, javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(tHeight, javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton1)
										.addComponent(jCheckBox1)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	// GEN-END:initComponents

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		try {
			String name = jTextField1.getText();
			int columns = Integer.parseInt(colNum.getText());
			int rows = Integer.parseInt(rowNum.getText());
			int pieceWidth = Integer.parseInt(tWidth.getText());
			int pieceHeight = Integer.parseInt(tHeight.getText());
			listener.onSet(name, columns, rows, pieceWidth, pieceHeight, jCheckBox1.isSelected());
			setVisible(false);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage(), e.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE);
		}

	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				MapParamSetDialog dialog = new MapParamSetDialog(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	public interface OnMapParamSetListener {
		public void onSet(String name, int columns, int rows, int pWidth, int pHeight, boolean used45Radian);
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTextField colNum;
	private javax.swing.JButton jButton1;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField rowNum;
	private javax.swing.JTextField tHeight;
	private javax.swing.JTextField tWidth;
	// End of variables declaration//GEN-END:variables

}